package SM.BusinessLogic;

public class SocietySetup {

	/*functions*/
	
	public void SocietyInfoUpdate(String societyName, String societyDescription) //update society name as only one
	{																			// soc exists in db at given time
		
		String UpdateQuery = "update SocietyInfo set name=? , description = ?";
		
		DBService.ExecuteQuery(UpdateQuery, societyName, societyDescription);
		
		/*Connection connection = null;
		PreparedStatement statement = null;
		
		try {
			connection = DBAccess.MakeConnection();
		
			statement = connection.prepareStatement(UpdateQuery);
			
			statement.setString(1, societyName);
			statement.setString(2, societyDescription);
			
			statement.executeUpdate();
			
		}catch(SQLException SQLEx)
		{
			System.out.println("SQLException in update soc info\nError : " + SQLEx.getMessage());
		}
		finally {
			
			DBAccess.CloseConnection(connection);
			DBAccess.CloseStatement(statement);
			
		}*/
	}
	
	public boolean IsBuildingPresent(String bCode)
	{
		String SelectQuery = "select * from BuildingCodes where building_code = ?";
		
		return DBService.IsRecordPresent(SelectQuery, bCode);
		
	}
	public void BuildingCodesInsert(String bName, String bCode, String bDesc) 
	{
		
		String InsertQuery = "insert into BuildingCodes(building_name, building_code, building_desc) values " +
				"(?,?,?)";
		
		DBService.ExecuteQuery(InsertQuery, bName, bCode, bDesc);
		
		/*Connection connection = null;
		PreparedStatement statement = null;
		
		try {
			connection = DBAccess.MakeConnection();
		
			statement = connection.prepareStatement(InsertQuery);
			
			statement.setString(1, bName);
			statement.setString(2, bCode);
			statement.setString(3, bDesc);
			
			statement.executeUpdate();
			
		}catch(SQLException SQLEx)
		{
			System.out.println("SQLException in building code insert\nError : " + SQLEx.getMessage());
		}
		finally {
			
			DBAccess.CloseConnection(connection);
			DBAccess.CloseStatement(statement);
			
		}*/
	}
	
	public boolean IsFlatNumberPresent(int flat_no)
	{
		String SelectQuery = "select * from FlatNumbers where flat_no = ?";
		
		return DBService.IsRecordPresent(SelectQuery, ""+flat_no);
	}
	
	public void FlatNumbersInsert(int flat_no) 
	{
		
		String InsertQuery = "insert into FlatNumbers values (?)";
		
		DBService.ExecuteQuery(InsertQuery, ""+flat_no);
		/*
		Connection connection = null;
		PreparedStatement statement = null;
		
		try {
			connection = DBAccess.MakeConnection();
		
			statement = connection.prepareStatement(InsertQuery);
			
			statement.setString(1, ""+flat_no);
			
			statement.executeUpdate();
			
		}catch(SQLException SQLEx)
		{
			System.out.println("SQLException in flat no insert\nError : " + SQLEx.getMessage());
		}
		finally {
			
			DBAccess.CloseConnection(connection);
			DBAccess.CloseStatement(statement);
			
		}*/
	}
	
	/*Following functions update member's data*/
	
	private boolean IsMemberPresent(String memberName)
	{
		String SelectQuery = "select * from CommitteeInfo where member_name = ?";
		
		return DBService.IsRecordPresent(SelectQuery, memberName);
	}
	
	public String AddMember(String memberName, String resp, String email, String contactNo)
	{
		
		if(IsMemberPresent(memberName))
		{
			return "<span style='color:red;'> Member " + memberName + " is " +
					"already present </span>";
		}
		
		String InsertQuery = "insert into CommitteeInfo values (?,?,?,?)";
		
		try
		{
			DBService.ExecuteQuery(InsertQuery, memberName, resp, email, contactNo);
			
			return "<span style='color:green;'>" + memberName + " is " +
			"now a committee member</span>";
			
		}catch(Exception Ex)
		{
			System.out.println("Exception in flat no insert\nError : " + Ex.getMessage());
		}
		
		/*Connection connection = null;
		PreparedStatement statement = null;
		
		try {
			connection = DBAccess.MakeConnection();
		
			statement = connection.prepareStatement(InsertQuery);
			
			statement.setString(1, memberName);
			statement.setString(2, resp);
			statement.setString(3, email);
			statement.setString(4, contactNo);
			
			statement.executeUpdate();
			
			return "<span style='color:green;'>" + memberName + " is " +
			"now a committee member</span>";
			
		}catch(SQLException SQLEx)
		{
			System.out.println("SQLException in flat no insert\nError : " + SQLEx.getMessage());
		}
		finally {
			
			DBAccess.CloseConnection(connection);
			DBAccess.CloseStatement(statement);
			
		}*/
		
		return "";
		
	}
	
	public void RemoveMember(String memberName)
	{
		String deleteQuery = "delete from CommitteeInfo where member_name = ?";
		
		DBService.ExecuteQuery(deleteQuery, memberName);
		
		/*Connection connection = null;
		PreparedStatement statement = null;
		
		try {
			connection = DBAccess.MakeConnection();
		
			statement = connection.prepareStatement(deleteQuery);
			
			statement.setString(1, memberName);
			
			statement.executeUpdate();
			
		}catch(SQLException SQLEx)
		{
			System.out.println("SQLException in is remove member\nError : " + SQLEx.getMessage());
		}
		finally {
			
			DBAccess.CloseConnection(connection);
			DBAccess.CloseStatement(statement);
			
		}*/
	}
	
	
}
